home *** CD-ROM | disk | FTP | other *** search
- Path: news1.urz.tu-dresden.de!news
- From: Hoang Minh Son <hoang@eatns1.et.tu-dresden.de>
- Newsgroups: comp.lang.c++
- Subject: Borland C++ 4.5 : delete [] operator - what's going on here?
- Date: 2 Feb 1996 10:33:58 GMT
- Organization: TU Dresden, IfA
- Message-ID: <4espam$ddf@rks1.urz.tu-dresden.de>
- NNTP-Posting-Host: 141.30.119.16
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.22 (Windows; I; 16bit)
-
- HI,
- I'm using EasyWin (Borland C++ 4.5) to test my matrix library under Windows 3.1.
- Could anybody tell me what's wrong in the following code?
-
- template<class T> class TMatrix
- {
- public:
- TMatrix(size_t m = 0, size_t n = 0);
- ~TMatrix()
- {
- delete[] elem;
- delete[] pcol;
- }
- T& operator()(size_t i, size_t j);
- {
- return pcol[j][i];
- }
-
- //....
- private:
- size_t nrow;
- size_t ncol;
- T** pcol; // pointer to columns
- T* elem; // element array
- };
-
- template<class T>
- TMatrix<T>::TMatrix(size_e m, size_t n) : nrow(m), ncol(n)
- {
- elem = new T[m*n+1]; // a dummy space for efficient use
- pcol = new T*[n+1]; // of 1-based indexing
-
- if (n > 0)
- {
- pcol[1] = elem;
- for (int i=1; i <= n; i++)
- pcol[i+1] = pcol[i] + m;
- }
- }
-
- typedef TMatrix<double> Matrix;
-
- void test()
- {
- Matrix a(4,4);
- for (int i = 1; i <= 4; i++)
- for (int j = 1; j <= 4; j++)
- a(i,j) = i + j;
- }
-
- main()
- {
- test();
-
- cout << " Oh! What's a trouble ";
- return 0;
- }
-
-
-
-
- The programm was build without trouble (You can test it by yourself). But as it was
- about to finish running, I got this runtime-error:
-
- " Unhandled Exception
- General Protection Exception
- 0X09FF...
- ........ Processor Faults "
-
- I debugged and discovered that the 'bug' may be caused on the destruction of the
- matrix 'a', before "Oh! What's a trouble" was displayed. However, if 'a' was
- declared outside 'test()', this "Oh!..." really came before the runtim error.
-
- What am I doing wrong? Or what's wrong with the delete [] operator?
-
-